package com.shycloud.mido.member.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.common.security.util.SecurityUtils;
import com.shycloud.mido.member.api.vo.OnlineCoursePeriodVO;
import com.shycloud.mido.member.api.vo.OnlineCourseRecordVO;
import com.shycloud.mido.member.api.vo.OnlineCourseRequestVO;
import com.shycloud.mido.member.api.vo.OnlineCourseScheduleVO;
import com.shycloud.mido.member.service.OnlineCourseService;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;


/**
 * @author Xiaoxing Yu
 * @date 2020/10/15
 **/

@RestController
@AllArgsConstructor
@RequestMapping("/online")
public class OnlineCourseController {

	//在线客服服务
	private OnlineCourseService onlineCourseService;

	/**
	 * 学生/教师进入房间
	 *
	 * @return com.shycloud.mido.common.core.util.R
	 * @author xiaoxing.yu
	 * @date 2020/10/15 09:49
	 */
	@PostMapping("/accessRoom")
	public R accessRoom(@RequestBody OnlineCourseRequestVO requestVO) {
		return onlineCourseService.accessRoom(getMemberId(), requestVO);
	}

	/**
	 * 学生/教师离开房间
	 *
	 * @return com.shycloud.mido.common.core.util.R
	 * @author xiaoxing.yu
	 * @date 2020/10/15 09:49
	 */
	@PostMapping("/exitRoom")
	public R exitRoom(@RequestBody OnlineCourseRequestVO requestVO) {
		return onlineCourseService.exitRoom(getMemberId(), requestVO);
	}

	/**
	 * 检查心跳状态并更新在线课消耗时间接口
	 * <p>
	 * NOTE: 目前前端APP会以1分钟一次的频率调用这个接口
	 *
	 * @return com.shycloud.mido.common.core.util.R
	 * @author xiaoxing.yu
	 * @date 2020/10/15 09:50
	 */
	@PostMapping("updateHeartbeat")
	public R updateHeartbeat(@RequestBody OnlineCourseRequestVO requestVO) {
		return onlineCourseService.updateHeartbeat(getMemberId(), requestVO);
	}

	/**
	 * 获取上课记录/点评
	 *
	 * @return com.shycloud.mido.common.core.util.R
	 * @author xiaoxing.yu
	 * @date 2020/10/15 09:50
	 */
	@GetMapping("getCourseRecord")
	public R getCourseRecord(String studentId, String teacherId, Integer year, Integer month) {
		return new R(onlineCourseService.getCourseRecord(studentId, teacherId, year, month));
	}

	/**
	 * 获取上课记录/点评
	 *
	 * @return com.shycloud.mido.common.core.util.R
	 * @author xiaoxing.yu
	 * @date 2020/10/15 09:50
	 */
	@GetMapping("getCourseDetailRecord")
	public R getCourseDetailRecord(String studentId, String teacherId, Integer year, Integer month) {
		return new R(onlineCourseService.getCourseDetailRecord(studentId, teacherId, year, month));
	}

	/**
	 * 分页获取上课记录/点评
	 *
	 * @return com.shycloud.mido.common.core.util.R
	 * @author xiaoxing.yu
	 * @date 2020/10/15 09:50
	 */
	@GetMapping("pageCoursePeriod")
	public R pageCoursePeriod(Page<OnlineCoursePeriodVO> page, String studentId, String teacherId) {
		return new R(onlineCourseService.pageCoursePeriodByTimeDesc(page, studentId, teacherId));
	}

	/**
	 * 获取当日上课记录/点评
	 *
	 * @return com.shycloud.mido.common.core.util.R
	 * @author xiaoxing.yu
	 * @date 2020/10/15 09:50
	 */
	@GetMapping("getCourseRecordByDate")
	public R getCourseRecordByDate(String studentId, String teacherId, String dateStr) {
		return new R(onlineCourseService.getCourseRecordByDate(studentId, teacherId, dateStr));
	}

	/**
	 * 增加上课记录与点评
	 *
	 * @return com.shycloud.mido.common.core.util.R
	 * @author xiaoxing.yu
	 * @date 2020/10/15 13:47
	 */
	@PostMapping("addCourseRecord")
	public R addAvailableOnlineCourseTime(@RequestBody OnlineCourseRecordVO recordVO) {
		return onlineCourseService.addCourseRecord(getMemberId(), recordVO);
	}

	/**
	 * 获取可用在线课时长(学生)
	 *
	 * @return com.shycloud.mido.common.core.util.R
	 * @author xiaoxing.yu
	 * @date 2020/10/15 14:49
	 */
	@GetMapping("getAvailTime")
	public R getAvailTime() {
		return new R(onlineCourseService.getAvailTime(getMemberId()));
	}

	/**
	 * 更新可用在线课时长(学生)
	 *
	 * @return com.shycloud.mido.common.core.util.R
	 * @author xiaoxing.yu
	 * @date 2020/10/15 14:49
	 */
	@PostMapping("updateAvailTime")
	public R updateAvailTime(@RequestBody String paramsJson) {
		JSONObject paramsJSONObject = JSONObject.parseObject(paramsJson);
		Integer delta = paramsJSONObject.getInteger("delta");
		String studentId = paramsJSONObject.getString("studentId");

		return new R(onlineCourseService.updateAvailTime(studentId, delta));
	}

	/**
	 * 获取累计在线课时长(学生)
	 *
	 * @return com.shycloud.mido.common.core.util.R
	 * @author xiaoxing.yu
	 * @date 2020/10/15 14:49
	 */
	@GetMapping("getAccuTime")
	public R getAccumulcateTime(String studentId) {
		return new R(onlineCourseService.getAccumulcateTime(studentId));
	}

	/**
	 * 增加累计在线课时长(学生)
	 *
	 * @return com.shycloud.mido.common.core.util.R
	 * @author xiaoxing.yu
	 * @date 2020/10/15 14:49
	 */
	@PostMapping("addAccuTime")
	public R addAccumulcateTime(@RequestBody String paramsJson) {
		JSONObject paramsJSONObject = JSONObject.parseObject(paramsJson);
		Integer delta = paramsJSONObject.getInteger("delta");
		String studentId = paramsJSONObject.getString("studentId");

		return new R(onlineCourseService.addAccumulcateTime(studentId, delta));
	}

	/**
	 * 获取可用在线课时长和累计在线课上课时长(学生)
	 *
	 * @return com.shycloud.mido.common.core.util.R
	 * @author xiaoxing.yu
	 * @date 2020/10/15 14:49
	 */
	@GetMapping("getStudentTime")
	public R getAvailTimeAndAccuTime(String studentId) {
		return new R(onlineCourseService.getAvailTimeAndAccuTime(studentId));
	}

	/**
	 * 获取可用在线课时长和累计在线课上课时长(type: 0-老师 1-学生)
	 *
	 * @return com.shycloud.mido.common.core.util.R
	 * @author xiaoxing.yu
	 * @date 2020/10/15 14:49
	 */
	@GetMapping("getStudentInfo")
	public R getStudentInfo(String studentId, String teacherId, Integer type) {
		return new R(onlineCourseService.getStudentInfo(studentId, teacherId, type));
	}

	/**
	 * 获取可用在线课时长和累计在线课上课时长(学生)
	 *
	 * @return com.shycloud.mido.common.core.util.R
	 * @author xiaoxing.yu
	 * @date 2020/10/15 14:49
	 */
	@GetMapping("getPeriodByDate")
	public R getPeriodByDate(String studentId, String teacherId, String dateStr) {
		return onlineCourseService.getPeriodByDate(studentId, teacherId, dateStr);
	}

	/**
	 * 检查对方是否获取过token
	 *
	 * @param teacherId @param studentId 	 @param type
	 * @return com.shycloud.mido.common.core.util.R
	 * @author xiaoxing.yu
	 * @date 2020/10/26 11:37 上午
	 */
	@GetMapping("checkIfHasToken")
	public R checkIfHasToken(String teacherId, String studentId, Integer type) {
		return new R(onlineCourseService.checkIfHasToken(teacherId, studentId, type));
	}

//	@GetMapping("checkLeavingStatus")
//	public R checkLeavingStatus(String teacherId, String studentId, Integer type) {
//		return new R(onlineCourseService.checkLeavingStatus(teacherId, studentId, type));
//	}

	private String getMemberId() {
		return SecurityUtils.getMember().getId();
	}

	/**
	 * 供测试用，重置心跳数据
	 */
	@PostMapping("reset")
	public R resetHeartBeatData(@RequestBody OnlineCourseRequestVO requestVO) {
		return onlineCourseService.resetHeartBeatData(requestVO);
	}

	/**
	 * 供测试用，重置心跳数据
	 */
	@PostMapping("init")
	public R init(@RequestBody OnlineCourseRequestVO requestVO) {
		if (requestVO.getType() == 2) {
			requestVO.setType(0);
			onlineCourseService.updateHeartbeat(SecurityUtils.getMember().getId(), requestVO);
			requestVO.setType(1);
			onlineCourseService.updateHeartbeat(SecurityUtils.getMember().getId(), requestVO);
		}

		return new R("ok");
	}

	/**
	 * 增加一条课程表记录
	 *
	 * @param requestVO
	 * @return com.shycloud.mido.common.core.util.R
	 * @author xiaoxing.yu
	 * @date 2020/12/22 上午9:35
	 */
	@PostMapping("addSchedule")
	public R addSchedule(@RequestBody OnlineCourseScheduleVO requestVO) {
		return onlineCourseService.addSchedule(requestVO);
	}

	/**
	 * 编辑一条课程表记录
	 *
	 * @param requestVO
	 * @return com.shycloud.mido.common.core.util.R
	 * @author xiaoxing.yu
	 * @date 2020/12/22 上午10:49
	 */
	@PostMapping("editSchedule")
	public R editSchedule(@RequestBody OnlineCourseScheduleVO requestVO) {
		return onlineCourseService.editSchedule(requestVO);
	}

	/**
	 * 删除一条课程表记录
	 *
	 * @param requestVO
	 * @return com.shycloud.mido.common.core.util.R
	 * @author xiaoxing.yu
	 * @date 2020/12/22 上午10:49
	 */
	@PostMapping("deleteSchedule")
	public R deleteSchedule(@RequestBody OnlineCourseScheduleVO requestVO) {
		return onlineCourseService.deleteSchedule(requestVO);
	}

	/**
	 * 获取一个月的课程表
	 *
	 * @param year
	 * @param month
	 * @param teacherId
	 * @param studentId
	 * @return com.shycloud.mido.common.core.util.R
	 * @author xiaoxing.yu
	 * @date 2020/12/22 上午9:36
	 */
	@GetMapping("getScheduleMonthly")
	public R getScheduleMonthly(Integer year, Integer month, String teacherId, String studentId) {
		return onlineCourseService.getScheduleMonthly(year, month, teacherId, studentId);
	}

	/**
	 * 获取一周的课程表
	 *
	 * @param mondayDateStr
	 * @param teacherId
	 * @param studentId
	 * @return com.shycloud.mido.common.core.util.R
	 * @author xiaoxing.yu
	 * @date 2020/12/22 上午10:53
	 */
	@GetMapping("getScheduleWeekly")
	public R getScheduleWeekly(String mondayDateStr, String teacherId, String studentId) {
		return onlineCourseService.getScheduleWeekly(mondayDateStr, teacherId, studentId);
	}

	/**
	 * 查询在线课体验信息(体验剩余时间，状态0-未领取 1-已领取)
	 *
	 * @return com.shycloud.mido.common.core.util.R
	 * @author xiaoxing.yu
	 * @date 2021/1/12 下午2:16
	 */
	@GetMapping("getTrialInfo")
	public R getTrialInfo() {
		String memberId = getMemberId();
		return new R(onlineCourseService.getTrialInfo(memberId));
	}

	/**
	 * 在线课体验领取
	 *
	 * @param
	 * @return com.shycloud.mido.common.core.util.R
	 * @author xiaoxing.yu
	 * @date 2021/1/12 下午2:38
	 */
	@PostMapping("claimTrial")
	public R claimTrial() {
		String memberId = getMemberId();
		return onlineCourseService.claimTrial(memberId);
	}

	/**
	 * 消耗体验课时间
	 *
	 * @param
	 * @return com.shycloud.mido.common.core.util.R
	 * @author xiaoxing.yu
	 * @date 1/13/21 1:34 PM
	 */
	@PostMapping("consumeTrialTime")
	public R consumeTrialTime(@RequestBody String paramsJson) {
		JSONObject paramsJSONObject = JSONObject.parseObject(paramsJson);
		Integer consumed = paramsJSONObject.getInteger("consumed");
		String memberId = getMemberId();
		return onlineCourseService.consumeTrialTime(memberId, consumed);
	}

	/**
	 * 在线体验课教师创建房间
	 *
	 * @param requestVO
	 * @return com.shycloud.mido.common.core.util.R
	 * @author xiaoxing.yu
	 * @date 1/15/21 1:34 PM
	 */
	@PostMapping("/createTrialRoom")
	public R createTrialRoom(@RequestBody OnlineCourseRequestVO requestVO) {
		return onlineCourseService.createTrialRoom(getMemberId(), requestVO);
	}

	/**
	 * 在线体验课学生检查房间存在状态和校验密码
	 *
	 * @param requestVO
	 * @return com.shycloud.mido.common.core.util.R
	 * @author xiaoxing.yu
	 * @date 1/19/21 2:28 PM
	 */
	@PostMapping("/checkTrialRoom")
	public R checkTrialRoom(@RequestBody OnlineCourseRequestVO requestVO) {
		return onlineCourseService.checkTrialRoom(getMemberId(), requestVO);
	}

	/**
	 * 在线体验课获取房间密码接口
	 *
	 * @param trialRoomShowId
	 * @return com.shycloud.mido.common.core.util.R
	 * @author xiaoxing.yu
	 * @date 1/19/21 2:38 PM
	 */
	@GetMapping("getTrialRoomPassword")
	public R getTrialRoomPassword(String trialRoomShowId) {
		return onlineCourseService.getTrialRoomPassword(trialRoomShowId);
	}

	/**
	 * 在线课体验分享后额外的体验课时间
	 *
	 * @param
	 * @return com.shycloud.mido.common.core.util.R
	 * @author xiaoxing.yu
	 * @date 1/15/21 5:30 PM
	 */
	@PostMapping("/claimTrialBonus")
	public R claimTrialBonus() {
		return onlineCourseService.claimTrialBonus(getMemberId());
	}

	/**
	 * 获取购买在线课课时列表
	 *
	 * @return {@link R}
	 * @author nianhua.jiang
	 * @date 2021/2/23 16:17
	 **/
	@GetMapping("/getBuyOnlineCourseList")
	public R getGoodsList() {
		return new R(onlineCourseService.getGoodsList());
	}

	//== 后台管理 在线课 统计 接口 ==========//
	@GetMapping("/getOrgTeacherCourseList")
	@ApiOperation(value = "根据机构ID获取机构下教师用在线课分钟数", tags = "后台在线课统计接口", notes = "测试 orgId = 2166 ,对接时请联系姜切换线上环境")
	public R getOrgTeacherCourseList(Integer orgId) {
		return new R(onlineCourseService.getOrgTeacherCourseList(orgId));
	}

	@GetMapping("/getTeacherCourseDetail")
	@ApiOperation(value = "获取教师上课详情", tags = "后台在线课统计接口", notes = "测试 teacherId = 452597ac7527e7b7e21c911d136fe708,对接时请联系姜切换线上环境")
	public R getTeacherCourseDetail(Integer current, Integer size, String teacherId) {
		return new R(onlineCourseService.getTeacherCourseDetail(new Page<>(current, size), teacherId));
	}

}
